package com.example.service.impl;

import com.example.Dao.GoodsDao;
import com.example.entity.Goods;
import com.example.service.GoodsService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    GoodsDao goodsDao;
    @Override
    public PageInfo<Goods> queryAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize );
        List<Goods> list=goodsDao.queryAll();
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int deleteByID(Integer id) {
        return goodsDao.deleteByID(id);
    }

    @Override
    public ArrayList<Goods> query() {
        return goodsDao.query();
    }


    @Cacheable(cacheNames = "goods")
    @Override
    public Goods findById(Integer id) {
        boolean flag = redisTemplate.hasKey("goods_"+id);
        if (flag) {
            // 从缓存查
            Object object = redisTemplate.opsForValue().get("goods_" + id);
            if (object != null) {
                return (Goods) object;
            }
        }
        // 从数据库中查
        Goods goods = goodsDao.findById(id);
        redisTemplate.opsForValue().set("goods_"+id,goods,1, TimeUnit.DAYS);
        return goods;
    }


    @Override
    public int update(Goods goods) {
        return goodsDao.update(goods);
    }

    @Override
    public int insert(Goods goods) {
        return goodsDao.insert(goods);
    }

    @Override
    public Goods foundByID(Integer gdID) {
        return goodsDao.foundByID(gdID);
    }

}

